package com.project.market.controller;

import com.project.market.db.domain.MarketAddress;
import com.project.market.service.AdminAddressService;
import com.project.market.service.impl.AdminAddressServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/address/*")
public class AdminAddressController extends HttpServlet {

    AdminAddressService adminAddressService = new AdminAddressServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/address/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取前端传递的参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        String name = req.getParameter("name");
        String userId = req.getParameter("userId");

        List<MarketAddress> list = adminAddressService.list(page, limit, sort, order, name, userId);

        if (list == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402, "参数值错误")));
        } else {
            Object ok = ResponseUtil.okList(list);
            String responseBody = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().println(responseBody);
        }
    }
}
